<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('fp.post.audit.status')" type="radio" v-model="baTable.form.items!.status"
                              prop="status" :input-attr="{ content: { '0': '待审核', '1': '审核通过', '2': '审核拒绝' } }"
                              :placeholder="t('Please select field', { field: t('fp.post.audit.status') })"/>
                    <FormItem
                        :label="$t('fp.post.audit.audit_reason')"
                        type="textarea"
                        :placeholder="$t('Please input field', { field: $t('fp.post.audit.audit_reason') })"
                        v-model="baTable.form.items!.audit_reason"
                        :input-attr="{ showWordLimit: true, maxlength: 120, rows: 3 }"
                    />
                    <FormItem :label="t('fp.post.audit.post_id')" type="string" prop="post_id"
                              :input-attr="{ step: 1, disabled:true}" v-model.number="baTable.form.items!.post_id"
                              :placeholder="t('Please input field', { field: t('fp.post.audit.post_id') })"/>

                    <FormItem v-if="baTable.form.items!.post.media.media_type === 'video'" label="视频标题" type="string"
                              prop="title" :input-attr="{disabled:true}"
                              v-model="baTable.form.items.post.title"/>
                    <FormItem label="内容" type="textarea" prop="title" :input-attr="{disabled:true}"
                              v-model="baTable.form.items!.post.content"/>

                    <el-form-item v-if="baTable.form.items!.post.media.media_type === 'video'" label="视频">

                        <video
                            ref="videoPlayer"
                            controls
                            :src="baTable.form.items!.post.media.media_url[0]"
                            style="width: 100%; height: auto;">
                        </video>

                    </el-form-item>
                    <el-form-item v-if="baTable.form.items!.post.media.media_type === 'video'" label="选中封面">
                        <div class="image-group">
                            <div class="image-item">
                                <img :src="baTable.form.items!.video_cover_sign_url" alt="图片"/>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item v-if="baTable.form.items!.post.media.media_type === 'video'" label="封面备选">
                        <div class="image-group">
                            <div
                                v-for="image in  baTable.form.items!.video_cover_url_bak"
                                :key="image.src"
                                class="image-item"
                                :class="{ selected: baTable.form.items!.video_cover_url === image.src }"
                                @click="selectImage(image)"
                            >
                                <img :src="image.url" alt="图片"/>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item label="图片" v-if="baTable.form.items!.post.media.media_type === 'image'">
                        <div class="image-group-2">

                            <el-image
                                v-for="(image, index) in baTable.form.items!.post.media.media_url"
                                :key="index"
                                class="image-item-2"
                                :src="image"
                                :zoom-rate="1.2"
                                :max-scale="7"
                                :min-scale="0.2"
                                :preview-src-list="[image]"
                                :initial-index="0"
                                alt="图片"
                            />
                        </div>
                    </el-form-item>
                </el-form>

            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>

</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({})

const selectImage = (image) => {
    baTable.form.items!.video_cover_url = image.src;
    baTable.form.items!.video_cover_sign_url = image.url;
};

</script>

<style scoped lang="scss">
.image-group {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    gap: 10px; /* 图片之间的间隔 */
}

.image-item {
    width: calc(20% - 8px); /* 每行 5 个图片，减去间距 */
    border: 2px solid transparent;
    cursor: pointer;
    transition: border-color 0.3s;
}

.image-item.selected {
    border-color: #409EFF;
}

.image-item img {
    width: 100%;
    height: 100%;
    object-fit: cover; /* 保持图片比例，裁剪 */
}


.image-group-2 {
    display: flex;
    flex-wrap: wrap; /* 允许换行 */
    gap: 10px; /* 图片之间的间隔 */
}

.image-item-2 {
    width: calc(33.333% - 8px); /* 每行 3 个图片，减去间距 */
    cursor: pointer;
    transition: transform 0.3s ease;
}

.image-item-2 img {
    width: 100%;
    height: 100%;
    object-fit: cover; /* 保持图片比例，裁剪 */
    border-radius: 8px;
}

</style>
